package com.qsl.ssyx.activity.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qsl.ssyx.activity.service.CouponInfoService;
import com.qsl.ssyx.common.result.Result;
import com.qsl.ssyx.model.activity.CouponInfo;
import com.qsl.ssyx.vo.activity.CouponRuleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 优惠券信息 前端控制器
 * </p>
 *
 * @author 青衫泪
 * @since 2023-08-28
 */
@Api(tags = "优惠券信息接口")
@RestController
@RequestMapping("/admin/activity/couponInfo")
public class CouponInfoController {

    @Autowired
    private CouponInfoService couponInfoService;


    @ApiOperation("条件分页查询")
    @GetMapping("/{page}/{limit}")
    public Result selectPage(@ApiParam(value = "当前页", required = true) @PathVariable Integer page,
                             @ApiParam(value = "每页记录数", required = true) @PathVariable Integer limit) {
        Page<CouponInfo> pageParam = new Page<>(page, limit);
        IPage<CouponInfo> model = couponInfoService.selectPage(pageParam);
        return Result.ok(model);
    }

    @ApiOperation("新增")
    @PostMapping("save")
    public Result save(@RequestBody CouponInfo couponInfo) {

        boolean flag = couponInfoService.save(couponInfo);
        return Result.ok(flag);
    }

    @ApiOperation("根据Id查询")
    @GetMapping("get/{id}")
    public Result getById(@ApiParam(value = "ActivityInfoId", required = true)
                          @PathVariable("id") Integer ActivityInfoId) {
        CouponInfo couponInfo = couponInfoService.getById(ActivityInfoId);
        return Result.ok(couponInfo);
    }

    @ApiOperation("修改")
    @PutMapping("update")
    public Result update(@RequestBody CouponInfo couponInfo) {
        boolean flag = couponInfoService.updateById(couponInfo);
        return Result.ok(flag);
    }

    @ApiOperation("删除")
    @DeleteMapping("remove/{Id}")
    public Result remove(@ApiParam("CouponInfoId") @PathVariable("Id") Integer couponInfoId) {

        boolean flag = couponInfoService.removeById(couponInfoId);
        return Result.ok(flag);
    }

    // ================优惠卷规则=================== findCouponRuleList
    @ApiOperation("根据优惠卷id获取优惠券规则列表") // 若规则为
    @GetMapping("findCouponRuleList/{id}")
    public Result findCouponRuleList(@ApiParam(value = "ActivityInfoId", required = true)
                                     @PathVariable("id") Long couponId) {
        Map<String, Object> map = couponInfoService.findCouponRuleList(couponId);
        return Result.ok(map);
    }

    @ApiOperation("新增优惠卷规则")
    @PostMapping("saveCouponRule")
    public Result saveCouponRule(@RequestBody CouponRuleVo couponRuleVo) {
        boolean flag = couponInfoService.saveCouponRule(couponRuleVo);
        return Result.ok(flag);
    }
}

